package com.ohos.ivbaranov.mfb.example.slice;

import com.ohos.ivbaranov.mfb.LogUtil;
import com.ohos.ivbaranov.mfb.MaterialFavoriteButton;
import com.ohos.ivbaranov.mfb.example.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.logging.Handler;

public class MainAbilitySlice extends AbilitySlice {
    private int niceCounterValue = 37;
    private MaterialFavoriteButton materialFavoriteButton;
    private MaterialFavoriteButton allFavorite;
    private Text counterValue;
    private StackLayout stackLayout;
    private Text clickText;
    private boolean isShowOrNot = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Window topWindow = WindowManager.getInstance().getTopWindow().get();
        WindowManager.LayoutConfig layoutConfig = new WindowManager.LayoutConfig();
//        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.BLUE.getValue()); // 设置状态栏颜色
//        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION);
//        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);//隐藏状态栏
//        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);//沉浸式状态栏
//        WindowManager.getInstance().getTopWindow().get().setStatusBarVisibility(1);   //设置状态栏显示


        stackLayout = (StackLayout) findComponentById(ResourceTable.Id_snake_bar);
        clickText = (Text) findComponentById(ResourceTable.Id_tool_bar_click);

        counterValue = (Text) findComponentById(ResourceTable.Id_counter_value);
        counterValue.setText(niceCounterValue);

        counterValue.setText(String.valueOf(niceCounterValue));
        allFavorite = (MaterialFavoriteButton) findComponentById(ResourceTable.Id_all_favorite);
        materialFavoriteButton = (MaterialFavoriteButton) findComponentById(ResourceTable.Id_favorite);
        materialFavoriteButton.setOnFavoriteChangeListener(new MaterialFavoriteButton.OnFavoriteChangeListener() {
            @Override
            public void onFavoriteChanged(MaterialFavoriteButton buttonView, boolean favorite) {
                if (favorite) {
                    niceCounterValue++;
                } else {
                    niceCounterValue--;
                }
                counterValue.setText(String.valueOf(niceCounterValue));
                LogUtil.error("onFavoriteChanged", "niceCounterValue: " + niceCounterValue);
                LogUtil.error("onFavoriteChanged", "favorite: " + favorite);
            }
        });

        materialFavoriteButton.setOnFavoriteAnimationEndListener(
                new MaterialFavoriteButton.OnFavoriteAnimationEndListener() {
                    @Override
                    public void onAnimationEnd(MaterialFavoriteButton buttonView, boolean favorite) {
                        counterValue.setText(String.valueOf(niceCounterValue));
                        LogUtil.error("onAnimationEnd", "niceCounterValue: " + niceCounterValue);
                    }
                });

        AnimatorProperty animatorProperty = stackLayout.createAnimatorProperty();
        animatorProperty.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(1000);


        allFavorite.setOnFavoriteChangeListener(new MaterialFavoriteButton.OnFavoriteChangeListener() {
            @Override
            public void onFavoriteChanged(MaterialFavoriteButton buttonView, boolean favorite) {
                clickText.setText(String.valueOf(favorite));
                stackLayout.setVisibility(Component.VISIBLE);
                new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                    @Override
                    public void run() {
                        stackLayout.setVisibility(Component.HIDE);
                    }
                }, 1000);
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
